//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CmsMarket.h"
using namespace Cephei::QL::Termstructures::Volatility::Swaption;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Indexes/SwapIndex.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Cashflows/HaganPricer.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
using namespace Cephei::QL::Cashflows;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::CCmsMarket (Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ swapLengths, Cephei::Core::IVector<Cephei::QL::Indexes::ISwapIndex^>^ swapIndexes, Cephei::QL::Indexes::IIborIndex^ iborIndex, Cephei::Core::IMatrix<Cephei::QL::IQuote^>^ bidAskSpreads, Cephei::Core::IVector<Cephei::QL::Cashflows::IHaganPricer^>^ pricers, Cephei::QL::Termstructures::IYieldTermStructure^ discountingTS) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CIborIndex^ _CiborIndex;
    CYieldTermStructure^ _CdiscountingTS;
    try
    {
#ifdef HANDLE
        _phCmsMarket = NULL;
#endif
        swapLengths ->Lock ();
        INativeVector<Cephei::QL::Times::IPeriod^>^ _NCIswapLengths = swapLengths->getFeature (NativeFeature::Value);
        CPeriodVector^ _NCswapLengths = safe_cast<CPeriodVector^>(_NCIswapLengths);
        std::vector<QuantLib::Period>& _swapLengths = static_cast<std::vector<QuantLib::Period>&> (_NCswapLengths->GetReference ());
        swapIndexes ->Lock ();
        INativeVector<Cephei::QL::Indexes::ISwapIndex^>^ _NCIswapIndexes = swapIndexes->getFeature (NativeFeature::shared_ptr);
        CSwapIndexVector^ _NCswapIndexes = safe_cast<CSwapIndexVector^>(_NCIswapIndexes);
        std::vector<boost::shared_ptr<QuantLib::SwapIndex> >& _swapIndexes = static_cast<std::vector<boost::shared_ptr<QuantLib::SwapIndex> >&> (_NCswapIndexes->GetShared ());
        _CiborIndex = safe_cast<CIborIndex^> (iborIndex);
        _CiborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _iborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CiborIndex->GetShared ()); 
        bidAskSpreads ->Lock ();
        INativeMatrix<Cephei::QL::IQuote^>^ _NCIbidAskSpreads = bidAskSpreads->getFeature (NativeFeature::Handle);
        CQuoteMatrix^ _NCbidAskSpreads = safe_cast<CQuoteMatrix^>(_NCIbidAskSpreads);
        std::vector<std::vector<Handle<QuantLib::Quote> > >& _bidAskSpreads = static_cast<std::vector<std::vector<Handle<QuantLib::Quote> > >&> (_NCbidAskSpreads->GetHandle ());
        pricers ->Lock ();
        INativeVector<Cephei::QL::Cashflows::IHaganPricer^>^ _NCIpricers = pricers->getFeature (NativeFeature::shared_ptr);
        CHaganPricerVector^ _NCpricers = safe_cast<CHaganPricerVector^>(_NCIpricers);
        std::vector<boost::shared_ptr<QuantLib::HaganPricer> >& _pricers = static_cast<std::vector<boost::shared_ptr<QuantLib::HaganPricer> >&> (_NCpricers->GetShared ());
        _CdiscountingTS = safe_cast<CYieldTermStructure^> (discountingTS);
        _CdiscountingTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountingTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountingTS->GetHandle ()); 
        _ppCmsMarket = new boost::shared_ptr<QuantLib::CmsMarket> (new QuantLib::CmsMarket ( _swapLengths,  _swapIndexes,  _iborIndex,  _bidAskSpreads,  _pricers,  _discountingTS ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (swapLengths != nullptr) swapLengths->Unlock();    //not optional
        if (swapIndexes != nullptr) swapIndexes->Unlock();    //not optional
        if (_CiborIndex != nullptr) _CiborIndex->Unlock();
        if (bidAskSpreads != nullptr) bidAskSpreads->Unlock();    //not optional
        if (pricers != nullptr) pricers->Unlock();    //not optional
        if (_CdiscountingTS != nullptr) _CdiscountingTS->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::CCmsMarket (boost::shared_ptr<QuantLib::CmsMarket>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCmsMarket = NULL;
#endif
	_ppCmsMarket = &childNative;
    
}
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::CCmsMarket (QuantLib::CmsMarket& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCmsMarket = NULL;
#endif
	_ppCmsMarket = new boost::shared_ptr<QuantLib::CmsMarket> (&childNative);
    
    _CmsMarketOwner = owner;
    
}

Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::CCmsMarket (CCmsMarket^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCmsMarket = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCmsMarket = new boost::shared_ptr<QuantLib::CmsMarket> (copy->GetShared());
        
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::CCmsMarket (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCmsMarket = NULL;
#endif
	if (!t->IsSubclassOf(CCmsMarket::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::CCmsMarket (QuantLib::Handle<QuantLib::CmsMarket>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCmsMarket = &childNative;
	_ppCmsMarket = &static_cast<boost::shared_ptr<QuantLib::CmsMarket>>(childNative.currentLink());
    
    _CmsMarketOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::CCmsMarket (QuantLib::Handle<QuantLib::CmsMarket> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCmsMarket = &childNative;
	_ppCmsMarket = &static_cast<boost::shared_ptr<QuantLib::CmsMarket>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::CCmsMarket (QuantLib::CmsMarket childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCmsMarket = NULL;
#endif
	_ppCmsMarket = new boost::shared_ptr<QuantLib::CmsMarket> (new QuantLib::CmsMarket (childNative));
    
}
#endif

Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::~CCmsMarket ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCmsMarket != NULL)
    {
	    delete _ppCmsMarket;
        _ppCmsMarket = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::!CCmsMarket ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCmsMarket != NULL)
    {
	    delete _ppCmsMarket;
    }
}
QuantLib::CmsMarket& Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::GetReference ()
{
    if (_ppCmsMarket == NULL) throw REFNEW NativeNullException ();
	return **_ppCmsMarket;
}
boost::shared_ptr<QuantLib::CmsMarket>& Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::GetShared ()
{
    if (_ppCmsMarket == NULL) throw REFNEW NativeNullException ();
	return *_ppCmsMarket;
}
QuantLib::CmsMarket* Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::GetPointer ()
{
    if (_ppCmsMarket == NULL) throw REFNEW NativeNullException ();
	return &**_ppCmsMarket;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CmsMarket>& Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::GetHandle ()
{
	if (_phCmsMarket == NULL)
	{
		_phCmsMarket = new Handle<QuantLib::CmsMarket> (*_ppCmsMarket);
	}
	return *_phCmsMarket;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::HasNative () 
{
	return (_ppCmsMarket != NULL);
}

Cephei::QL::Math::IMatrix^ Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::Browse::get ()
{
    try
    {
    	QuantLib::Matrix _rv = (QuantLib::Matrix)(*_ppCmsMarket)->browse ( );   
        Cephei::QL::Math::CMatrix^ _nrv = REFNEW Cephei::QL::Math::CMatrix (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::ImpliedCmsSpreads::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppCmsMarket)->impliedCmsSpreads ( );   
        Cephei::QL::Math::CMatrix^ _nrv = REFNEW Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::SpreadErrors::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppCmsMarket)->spreadErrors ( );   
        Cephei::QL::Math::CMatrix^ _nrv = REFNEW Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::SwapTenors::get ()
{
    try
    {
    	std::vector<QuantLib::Period>& _rv = (std::vector<QuantLib::Period>&)(*_ppCmsMarket)->swapTenors ( );   
        CoVector<Cephei::QL::Times::IPeriod^>^ _nrv = REFNEW CoVector<Cephei::QL::Times::IPeriod^>(REFNEW CPeriodVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::ICmsMarket^ Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppCmsMarket)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::WeightedFwdNpvError (Cephei::QL::Math::IMatrix^ weights)
{
    CMatrix^ _Cweights;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cweights = safe_cast<CMatrix^> (weights);
        _Cweights->Lock();
        QuantLib::Matrix& _weights = static_cast<QuantLib::Matrix&> (_Cweights->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCmsMarket)->weightedFwdNpvError ( _weights );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cweights != nullptr) _Cweights->Unlock();
    }
}
Double Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::WeightedSpotNpvError (Cephei::QL::Math::IMatrix^ weights)
{
    CMatrix^ _Cweights;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cweights = safe_cast<CMatrix^> (weights);
        _Cweights->Lock();
        QuantLib::Matrix& _weights = static_cast<QuantLib::Matrix&> (_Cweights->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCmsMarket)->weightedSpotNpvError ( _weights );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cweights != nullptr) _Cweights->Unlock();
    }
}
Double Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket::WeightedSpreadError (Cephei::QL::Math::IMatrix^ weights)
{
    CMatrix^ _Cweights;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cweights = safe_cast<CMatrix^> (weights);
        _Cweights->Lock();
        QuantLib::Matrix& _weights = static_cast<QuantLib::Matrix&> (_Cweights->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCmsMarket)->weightedSpreadError ( _weights );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cweights != nullptr) _Cweights->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Swaption::ICmsMarket^ Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarket_Factory::Create (Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ swapLengths, Cephei::Core::IVector<Cephei::QL::Indexes::ISwapIndex^>^ swapIndexes, Cephei::QL::Indexes::IIborIndex^ iborIndex, Cephei::Core::IMatrix<Cephei::QL::IQuote^>^ bidAskSpreads, Cephei::Core::IVector<Cephei::QL::Cashflows::IHaganPricer^>^ pricers, Cephei::QL::Termstructures::IYieldTermStructure^ discountingTS)
{
    return REFNEW CCmsMarket ( swapLengths,  swapIndexes,  iborIndex,  bidAskSpreads,  pricers,  discountingTS);
}
